package deconstruction.deconTable;

import java.util.ArrayList;

import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

public class SlotInput extends Slot
{
	/** The craft matrix inventory linked to this result slot. */
	private final IInventory craftMatrix;

	/** The player that is using the GUI where this slot resides. */
	private EntityPlayer thePlayer;

	/**
	 * The number of items that have been crafted so far. Gets passed to ItemStack.onCrafting before being reset.
	 */
	private int amountCrafted;

	private IInventory enchantInv;
	private int pickupCount = 0;

	private Container container;
	public SlotInput(EntityPlayer par1EntityPlayer, IInventory par2IInventory, IInventory par3IInventory, int par4, int par5, int par6, IInventory par7IInventory, Container par8Container)
	{
		super(par3IInventory, par4, par5, par6);
		this.thePlayer = par1EntityPlayer;
		this.craftMatrix = par2IInventory;
		this.enchantInv =  par7IInventory;
		this.container = par8Container;
	}

	/**
	 * Check if the stack is a valid item for this slot. Always true beside for the armor slots.
	 */
	public boolean isItemValid(ItemStack par1ItemStack)
	{
		return false;
	}

	/**
	 * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
	 * stack.
	 */
	public ItemStack decrStackSize(int par1)
	{
		if (this.getHasStack())
		{
			this.amountCrafted += Math.min(par1, this.getStack().stackSize);
		}

		return super.decrStackSize(par1);
	}

	/**
	 * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an
	 * internal count then calls onCrafting(item).
	 */
	protected void onCrafting(ItemStack par1ItemStack, int par2)
	{
		this.amountCrafted += par2;
		this.onCrafting(par1ItemStack);
	}

	/**
	 * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood.
	 */
	protected void onCrafting(ItemStack par1ItemStack)
	{

	}

	public void onPickupFromSlot(EntityPlayer par1EntityPlayer, ItemStack par2ItemStack)
	{


		for (int i = 0; i < this.craftMatrix.getSizeInventory(); ++i)
		{
			ItemStack itemstack1 = this.craftMatrix.getStackInSlot(i);

			if (itemstack1 != null)
			{
				if(enchantInv.getStackInSlot(0) != null && enchantInv.getStackInSlot(0).itemID == Item.book.itemID && itemstack1.isItemEnchanted()){
					enchantInv.setInventorySlotContents(0, DeconstructionManager.getInstance().undoEnchant(itemstack1));
				}
				ArrayList<ItemStack> list = new ArrayList<ItemStack>();
				for(i=0; i<this.inventory.getSizeInventory(); ++i){
					if(this.inventory.getStackInSlot(i) != null)
						list.add(this.inventory.getStackInSlot(i));
				}
				
				for(i = 0; i<this.inventory.getSizeInventory();i++){
					if(this.inventory.getStackInSlot(i) != null){
						EntityItem entityitem = new EntityItem(this.thePlayer.worldObj, this.thePlayer.posX, this.thePlayer.posY - 0.30000001192092896D + (double)this.thePlayer.getEyeHeight(), this.thePlayer.posZ, this.inventory.getStackInSlot(i));
						entityitem.delayBeforeCanPickup = 0;

						this.thePlayer.joinEntityItemWithWorld(entityitem);
						this.inventory.setInventorySlotContents(i, null);
					}
				}
				this.craftMatrix.decrStackSize(0, DeconstructionManager.getInstance().getItemStackSize());
				this.container.onCraftMatrixChanged(this.craftMatrix);
				this.container.onCraftMatrixChanged(this.craftMatrix);

				//TODO: Fix decrStackSize,             	


			}
		}
	}
}
